﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BLSwindles;
using OBSwindles;
using System.Collections.Generic;
using UTSwindles;

namespace Swindles.Customer
{
    public partial class CustomerDetails : System.Web.UI.Page
    {
        private bool isEmployee
        {
            get
            {
                return (Utilities.getBoolean(User.IsInRole("Employee")) ? true : false);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadCustomerDetails();
            }
            if (isEmployee)
            {
                tblEmployee.Visible = true;
            }
            else
            {
                tblEmployee.Visible = false;
            }

        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            grdLoan.ItemDataBound += new DataGridItemEventHandler(grdLoan_ItemDataBound);
            grdAccounts.ItemDataBound += new DataGridItemEventHandler(grdAccounts_ItemDataBound);
            grdLoan.EditCommand += new DataGridCommandEventHandler(grdLoan_EditCommand);
            grdLoan.CancelCommand += new DataGridCommandEventHandler(grdLoan_CancelCommand);
            grdLoan.UpdateCommand += new DataGridCommandEventHandler(grdLoan_UpdateCommand);
        }

        void grdAccounts_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                OBSwindles.AccountInfo accountInfo = (AccountInfo)e.Item.DataItem;
                if (accountInfo != null)
                {
                    e.Item.Cells[4].Text = accountInfo.AccountOfficer.FullName;
                }

            }
        }

        void grdLoan_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                OBSwindles.LoanInfo loanInfo = (LoanInfo)e.Item.DataItem;
                if (loanInfo != null)
                {
                    e.Item.Cells[6].Text = loanInfo.LoanOfficer.FullName;
                }

            }
        }

        void grdLoan_UpdateCommand(object source, DataGridCommandEventArgs e)
        {
            Control control;
            //customerID
            int LoanID = Convert.ToInt32(e.Item.Cells[0].Text);
            if (LoanID > 0)
            {
                LoanInfo loanInfo = BLFactory.getLoan_ByID(LoanID);
                //LoanType
                control = e.Item.Cells[1].Controls[0];
                if (control != null)
                {
                    loanInfo.LoanType = ((TextBox)control).Text;
                }
                //LoanAmount
                control = e.Item.Cells[2].Controls[0];
                if (control != null)
                {
                    loanInfo.LoanAmount = Utilities.getFloat(((TextBox)control).Text);
                }
                //LoanIntrestRate
                control = e.Item.Cells[3].Controls[0];
                if (control != null)
                {
                    loanInfo.LoanIntrestRate = Utilities.getFloat(((TextBox)control).Text);
                }
                //LoanTerm
                control = e.Item.Cells[4].Controls[0];
                if (control != null)
                {
                    loanInfo.LoanTerm = Utilities.getInt32(((TextBox)control).Text);
                }
                //LoanStartDateString - readonly
                //Loan Officer -  readonly
                //LoanRepaymentCurrent
                control = e.Item.Cells[7].Controls[0];
                if (control != null)
                {
                    loanInfo.LoanRepaymentCurrent = Utilities.getFloat(((TextBox)control).Text);
                }

                BLFactory.updateLoan(loanInfo);
            }
            grdLoan.EditItemIndex = -1;
            LoadCustomerDetails();
        }

        void grdLoan_CancelCommand(object source, DataGridCommandEventArgs e)
        {
            grdLoan.EditItemIndex = -1;
            LoadCustomerDetails();
        }

        void grdLoan_EditCommand(object source, DataGridCommandEventArgs e)
        {
            grdLoan.EditItemIndex = e.Item.ItemIndex;
            LoadCustomerDetails();
        }


        



        private void LoadCustomerDetails()
        {
            string userName = User.Identity.Name;
            string customerID = Utilities.getString(Request.QueryString.Get("id"));
            OBSwindles.Customer customer = new OBSwindles.Customer();
            if (isEmployee)
            {
                customer = BLFactory.getCustomer_ByID(Utilities.getInt32(customerID));
            }
            else
            {
                customer = BLFactory.getCustomer_ByLoginUser(userName);
            }
            
            if (customer != null)
            {
                lblCustomerName.Text = string.Concat(customer.FirstName, " ", customer.LastName);
                lblCustomerNumber.Text = customer.CustomerNo;
                lblContactNumber.Text = customer.ContactNumber;
                lblContactAddress.Text = customer.ContactAddress;
                IList<AccountInfo> accountList = (IList<AccountInfo>)customer.AccountList;
                grdAccounts.DataSource = accountList;
                grdAccounts.DataBind();
                IList<LoanInfo> loanList = (IList<LoanInfo>)customer.LoanList;
                grdLoan.DataSource = loanList;
                grdLoan.DataBind();
            }
            if (!isEmployee)
            {
                grdAccounts.Columns[5].Visible = false;
                grdLoan.Columns[9].Visible = false;
            }
        }
    }
}
